unit uDZProcess;

interface

uses
  Classes, uDZSysUtils, uDZPersistent;

type
  TDzAppLogType = (altFile, altConsole);
  TDzExecuteMode = (pemNormal, pemService, pemDriver);
  TDzCmdLineParams = class(TDzPersistent)
  private
    procedure ParseCommandLine(const CmdLine: string);
  protected
    procedure ParseParam(const param: string); virtual;
  public
    constructor Create(const CmdLine: string); reintroduce; overload;
    procedure Parse(const param: string);
    destructor Destroy; override;
  end;

procedure ParseCmdLine(Params: TDzCmdLineParams);

implementation

procedure ParseCmdLine(Params: TDzCmdLineParams);
var
  i: Integer;
begin
  for i := 1 to ParamCount do Params.Parse(ParamStr(i));
end;

{ TDzCmdLineParams }

constructor TDzCmdLineParams.Create(const CmdLine: string);
begin
  inherited Create;
  ParseCommandLine(CmdLine);
end;

destructor TDzCmdLineParams.Destroy;
begin

  inherited;
end;

procedure TDzCmdLineParams.Parse(const param: string);
var
  key, value: string;
  sym: Integer;
begin
  if Length(param) < 2 then
  begin
    ParseParam(param);
    Exit;
  end;

  if param[1] <> '-' then
  begin
    ParseParam(param);
    Exit;
  end;
  
  if param[2] <> '-' then
  begin
    ParseParam(param);
    Exit;
  end;

  if Length(param) < 3 then
  begin
    ParseParam(param);
    Exit;
  end;

  sym := Pos('=', param);
  if sym > 0 then
  begin
    key := Copy(param, 3, sym - 3);
    value := Copy(param, sym + 1, MaxInt);
  end
  else begin
    key := Copy(param, 3, MaxInt);
    value := 'True';
  end;

  try
    Self.Attributes[key] := value;
  except
    ParseParam(param);
  end;
end;

procedure TDzCmdLineParams.ParseCommandLine(const CmdLine: string);
var
  params: TStrings;
  i: Integer;
begin
  params := TStringList.Create;
  try
    params.Delimiter := #32;
    params.DelimitedText := CmdLine;
    for i := 1 to params.Count - 1 do Parse(params[i]);
  finally
    params.Free;
  end;
end;

procedure TDzCmdLineParams.ParseParam(const param: string);
begin

end;

end.
